package com.micromsg.basic.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //路径"/ws"被注册为STOMP端点，对外暴露，客户端通过该路径接入WebSocket服务
        registry.addEndpoint("/ws")
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 用户可以订阅来自以"/topic"为前缀的消息，客户端只可以订阅这个前缀的主题
//        config.enableSimpleBroker("/topic");

        // 客户端发送过来的消息，需要以"/app"为前缀，再经过Broker转发给响应的Controller
        config.setApplicationDestinationPrefixes("/app");
    }
}
